package com.example.passport.mapper;

import com.example.common.dto.Pagination;
import com.example.passport.dto.MenuSearchDto;
import com.example.passport.entity.Menu;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MenuMapper {

    @Insert("INSERT INTO `menu`" +
            " (`menuName`,`parent_id`,`order_num`,`url`,`menu_type`,`visible`,`perms`,`icon`,`create_by`,`update_by`,`remark`) VALUES" +
            " (#{menuName}, #{parentId}, #{orderNum}, #{url}, #{menu_type}, #{visible},#{perms},#{icon},#{createBy},#{updateBy},#{remark})")
    void create(Menu menu);

    @Insert("INSERT INTO `role_menu`" +
            " (`role_id`,`menu_id`) VALUES" +
            " (#{roleId}, #{menuId})")
    void createRoleMenu(@Param("roleId") Long role_id,@Param("menuId") Long menu_id);


    @Select("<script>" +
            "SELECT COUNT(*) FROM menu as m,role as r,role_menu as rm,user_role as ur " +
            "WHERE m.id=rm.menu_id and r.id=rm.role_id and ur.user_id=#{userId} and ur.role_id=r.id and m.url=#{url}"+
            "</script>")
    int isPermission(@Param("userId") Long userId,@Param("url") String url);

    @Select("<script>" +
            "    SELECT m.* FROM menu as m,role as r,role_menu as rm,user_role as ur" +
            "    <where>" +
            "      <if test='menu.userId!=null'>and ur.user_id=#{menu.userId}</if>" +
            "      <if test='menu.starts!=null'>and rm.starts=#{menu.starts}</if>" +
            "      <if test='menu.menuType!=null'>and m.menu_type=#{menu.menuType}</if>" +
            "      <if test='menu.parentId!=null'>and m.parent_id=#{menu.parentId}</if>" +
            "      and m.id=rm.menu_id and r.id=rm.role_id and ur.role_id=r.id" +
            "    </where>"+
            "</script>")
    List<Menu> findAll(@Param("menu") MenuSearchDto menu);

    @Select("select * from menu")
    List<Menu> find();

    @Select("select menu_id from role_menu where role_id=#{roleId} and menu_id>1000 and menu_id <9999")
    List<Long> findId(@Param("roleId")Long roleId);

    @Delete("delete from role_menu where role_id=#{roleId}")
    void deleteId (@Param("roleId") Long roleId);
}
